package com.cn.mobileproject.view.dialog;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.cn.mobileproject.R;
import com.cn.mobileproject.activity.LoginActivity;
import com.cn.mobileproject.data.GlobalValue;
import com.cn.mobileproject.data.SystemParams;

public class ExitDialog extends AlertDialog implements View.OnClickListener{

	private Button cancelBtn;
	private Button logoutBtn;
	private Button exitBtn;
	private Context mContext;
	private int mOp;

	public ExitDialog(Context context,int op) {
		super(context);
		mContext = context;
		mOp = op;
	}
	
	public ExitDialog(Context context) {
		super(context);
		mContext = context;
		mOp = Gravity.BOTTOM;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_main);
		Window window = getWindow();
		if (mOp == Gravity.TOP) {
			window.setGravity(Gravity.TOP);
			window.setWindowAnimations(R.style.mystyle_top);
		}else if (mOp == Gravity.LEFT) {
			window.setGravity(Gravity.LEFT);
			window.setWindowAnimations(R.style.mystyle_left);
		}else if(mOp ==Gravity.RIGHT){
			window.setGravity(Gravity.RIGHT);
			window.setWindowAnimations(R.style.mystyle_right);
		}else{
			window.setGravity(Gravity.BOTTOM);
			window.setWindowAnimations(R.style.mystyle_bottom);
		}
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.alpha = 0.97f;
		lp.dimAmount = 0.7f;
		window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		initComponent();
		initEvent();
	}
	
	private void initComponent(){
		cancelBtn = (Button) findViewById(R.id.dialog_btn_cancel);
		exitBtn = (Button) findViewById(R.id.dialog_btn_exit);
		logoutBtn =  (Button) findViewById(R.id.dialog_btn_logout);
	}
	
	private void initEvent(){
		cancelBtn.setOnClickListener(this);
		logoutBtn.setOnClickListener(this);
		exitBtn.setOnClickListener(this);
	}
	
	private void exit(){
		dismiss();
		ActivityManager am = (ActivityManager) mContext.getSystemService(Activity.ACTIVITY_SERVICE);
		try {
			am.restartPackage(mContext.getPackageName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		android.os.Process.killProcess(android.os.Process.myPid());
		((Activity) mContext).finish();
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.dialog_btn_cancel:
			dismiss();
			break;
		case R.id.dialog_btn_exit:
			exit();
			break;
		case R.id.dialog_btn_logout:
			logout();
			break;
		}		
	}
	
	private void logout(){
		dismiss();
		GlobalValue.getInstance().clearInstructs();
		SystemParams.reSetValue();
		Intent intent = new Intent(mContext,LoginActivity.class);
		intent.putExtra("logout", true);
		mContext.startActivity(intent);
		((Activity) mContext).finish();
	}
}
